import QtQuick 2.0
import "../components"
import "../sampleManager"
import "../dataAnalysis"
import "../program"
Rectangle {
    id: root

    anchors.fill: parent
    property int currentPage: 0

    property string expGuid: ""

    property bool initAnalysis: false
    property var gProgramInfo: []
    property var gProgramStep: []

    property bool isEditingSamplePannel: false

    Component.onCompleted: {
        // 初始化按钮样式
        var list = layout_tab.children
        for(let i = 0; i < list.length; i ++){
            list[i].button_color = "transparent"
            list[i].text_color = "#666666"
            list[i].button_color_select = "white"
            list[i].text_color_select = "#1677FF"
        }
    }

    BoxColorPicker{
        id: boxColorPicker
        onAccept: {
            if(mode === 0){
//                var colorRes = [colorList["FAM"], colorList["VIC"], colorList["ROX"], colorList["CY5"]]
//                CAnalysis.setColor(colorRes)
                CAnalysis.setColor(colorList)
            }else if(mode === 1){
                CAnalysis.setColor_posi(colorList)
            }
            dataAnalysis.reloadChartData()

        }
    }

    function open(){

        sampleManager.initSampleList()
        sampleManager.setSampleList(CWlk.getSampleInfo(expGuid))
        isEditingSamplePannel = false

        var expInfo = CWlk.getExperimentInfo(expGuid)
        if(expInfo["ExperimentName"]===undefined){
            mainwindow.showToast("文件格式不正确", 1);
        }
        experimentInfo.name = expInfo["ExperimentName"]
        experimentInfo.program = expInfo["AmplName"]
        experimentInfo.code = expInfo["Code"]
        experimentInfo.kit = expInfo["TestKit"]
        experimentInfo.system = expInfo["System"]
        experimentInfo.consume = expInfo["ConsumableType"]

        gProgramInfo = CAmplProgram.getProgramInfo("1")[0]
        gProgramStep = CAmplProgram.getProgramStepInfo(gProgramInfo["Id"], "1")
        programDetails.setProgramInfo(gProgramInfo)
        programDetails.setProgramStep(gProgramStep)

        dataAnalysis.tabAnalysis("")
        currentPage = 0
    }


    function tabPage(idx){

        currentPage = idx

    }


    Rectangle{          // 切换： 实验信息 | 样本板 | 结果 =================================

        height: 60*dp
        width: parent.width
        color: "#EFEFF2"
        Row{
            id: layout_tab
            anchors.fill: parent
            PushButton{
                height: 60*dp
                width: 120*dp
                title: "实验信息"
                isSelected: currentPage === 0
                onReleased: {
                    tabPage(0)
                }
            }PushButton{
                height: 60*dp
                width: 120*dp
                title: "样本板"
                isSelected: currentPage === 1
                onReleased: {
                    tabPage(1)
                }
            }PushButton{
                height: 60*dp
                width: 120*dp
                title: "扩增程序"
                isSelected: currentPage === 3
                onReleased: {
                    tabPage(3)
                }
            }PushButton{
                height: 60*dp
                width: 120*dp
                title: "结果"
                isSelected: currentPage === 2
                onReleased: {
                    tabPage(2)
                }
            }
        }
    }

    Item{
        id: layout_page
        y: 60*dp
        height: root.height - 120*dp
        width: root.width


        ExperimentInfo{
            id: experimentInfo
            anchors.fill: parent
            visible: currentPage === 0
        }

        SampleManager{
            id: sampleManager
            isEditable: isEditingSamplePannel
            anchors.fill: parent
            visible: currentPage === 1
        }

        DataAnalysis{
            id: dataAnalysis
            anchors.fill: parent
            visible: currentPage === 2
        }
        ProgramDetails{
            id: programDetails
            anchors.fill: parent
            editable: 0
            visible: currentPage === 3
        }
    }
    Rectangle{          // 下面的按钮 -------------------------------------------------
        id: layout_button
        y: parent.height-60*dp
        height: 60*dp
        width: parent.width

        Item{                   // Page0 实验信息
            anchors.fill: parent
            visible: currentPage === 0
            PushButton{
                y: 10*dp
                x: parent.width-140*dp
                title: "下一步"
                defaultMode2: true
                onReleased: {
                    tabPage(1)
                }
            }
        }
        Item{                   // Page1 样本板
            anchors.fill: parent
            visible: currentPage === 1
            PushButton{
                y: 10*dp
                x: 20*dp
                title: "上一步"
                defaultMode2: true
                onReleased: {
                    tabPage(0)
                }
            }PushButton{
                y: 10*dp
                x: parent.width-275*dp
                title: isEditingSamplePannel ? "保存" : "重新编辑"
                defaultMode2: true
                onReleased: {
                    isEditingSamplePannel = !isEditingSamplePannel
                }
            }PushButton{
                y: 10*dp
                x: parent.width-140*dp
                title: "下一步"
                defaultMode2: true
                onReleased: {
                    tabPage(3)
                }
            }
        }
        Item{                   // Page3 结果
            anchors.fill: parent
            visible: currentPage === 2
            PushButton{
                y: 10*dp
                x: 20*dp
                title: "颜色配置"
                defaultMode2: true
                onReleased: {
                    boxColorPicker.show()
                }
            }PushButton{
                y: 10*dp
                x: dataAnalysis.curPage === 3 ? parent.width-280*dp : parent.width-420*dp
                title: "分析配置"
                defaultMode2: true
                onReleased: {
                    dataAnalysis.analysisConfig()
                }
            }PushButton{
                y: 10*dp
                x: parent.width-280*dp
                title: "导出图片"
                visible: dataAnalysis.curPage !== 3
                defaultMode2: true
                onReleased: {
                    var res = CAnalysis.exportImage()
                    if(res === 1){
                        mainwindow.showToast("导出成功", 0)
                    }else if(res === 0){
                        mainwindow.showToast("导出失败", 1)
                    }
                }
            }PushButton{
                y: 10*dp
                x: parent.width-140*dp
                title: "导出报告"
                defaultMode1: true
                onReleased: {
                    var res = CAnalysis.datao()
                    if(res === 1){
                        mainwindow.showToast("导出成功", 0)
                    }else if(res === 0){
                        mainwindow.showToast("导出失败", 1)
                    }
                }
            }
        }

        CustomBorder{
            tBorderwidth: 1
            borderColor: "#EFEFEF"
        }
    }       // 按钮定义结束 -----------------------------------------------------------
}
